package io.hepu.robotize.controller;

import io.hepu.robotize.base.BaseController;
import io.hepu.robotize.http.HttpResponse;
import io.hepu.robotize.model.Permission;
import io.hepu.robotize.service.IPermissionService;
import io.hepu.robotize.util.Limits;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "权限管理")
@RestController
@RequestMapping("/permission")
public class PermissionController extends BaseController<Permission, String, IPermissionService> {

    @Operation(summary = "查询兄弟节点最后的ID值", description = "返回ID值")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "请求成功"),
            @ApiResponse(responseCode = "403", description = "禁止访问")
    })
    @GetMapping("/lastId/{id}")
    public HttpResponse<Object> getLastId(@PathVariable("id") String id) {
        List<Permission> permissions = getService().findByParentId(id);
        return HttpResponse.success(Limits.nonEmpty(permissions) ? id + (permissions.size() + 1) : id + 1);
    }
}
